package com.xyd.service.impl;

import com.xyd.entity.Comment;
import com.xyd.entity.PageBean;
import com.xyd.mapper.CommentMapper;
import com.xyd.service.CommentService;
import com.xyd.util.MybatisUtil;
import com.xyd.util.TotalPageUtil;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/1/9 17:17
 */
public class CommentServiceImpl implements CommentService {

  private SqlSession sqlSession;

  @Override
  public Boolean addComment(Comment comment) {
    sqlSession = MybatisUtil.getSqlSession(true);
    Boolean flag = sqlSession.getMapper(CommentMapper.class).addComment(comment);
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public PageBean<Comment> findComment(Integer currentPage) {
    PageBean<Comment> pb = new PageBean<>();

    //查询留言总条数
    sqlSession = MybatisUtil.getSqlSession();
    Integer totalCount = sqlSession.getMapper(CommentMapper.class).findCountComment();
    MybatisUtil.closeSqlSession(sqlSession);
    pb.setTotalCount(totalCount);

    // 每页显示的条数
    Integer pageSize = 5;
    pb.setPageSize(pageSize);

    //  计算总页数
    sqlSession = MybatisUtil.getSqlSession();
    pb.setTotalPage(TotalPageUtil.getTotalPage(totalCount, pageSize));
    MybatisUtil.closeSqlSession(sqlSession);

    // 当前页码
    pb.setCurrentPage(currentPage);

    //每页显示的数据
    sqlSession = MybatisUtil.getSqlSession();
    List<Comment> comments = sqlSession.getMapper(CommentMapper.class)
        .findComments((currentPage - 1) * pageSize, pageSize);
    MybatisUtil.closeSqlSession(sqlSession);
    pb.setList(comments);

    return pb;
  }

  /**
   *
   * @param start    开始的条数
   * @param pageSize 每页显示的条数
   * @return
   */
  @Override
  public List<Comment> findComments(Integer start, Integer pageSize) {
    sqlSession=MybatisUtil.getSqlSession();
    List<Comment>commentList=sqlSession.getMapper(CommentMapper.class).findComments(start,pageSize);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return commentList;
  }

  @Override
  public Integer findCountComment() {
    sqlSession=MybatisUtil.getSqlSession();
    int count=sqlSession.getMapper(CommentMapper.class).findCountComment();
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return count;
  }

  @Override
  public Boolean delComment(Long ecId) {
    sqlSession=MybatisUtil.getSqlSession();
    Boolean flag=sqlSession.getMapper(CommentMapper.class).delComment(ecId);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public List<Comment> CommentByPage(Integer pageIndex, Integer pageSize) {
    sqlSession=MybatisUtil.getSqlSession();
    List<Comment>commentList=sqlSession.getMapper(CommentMapper.class).CommentByPage(pageIndex,pageSize);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return commentList;
  }

  /**
   *
   * @param ecId
   * @return
   */
  @Override
  public Comment getAllCommentById(Long ecId) {
    sqlSession=MybatisUtil.getSqlSession();
    Comment comment=sqlSession.getMapper(CommentMapper.class).getAllCommentById(ecId);

    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return comment;
  }

  @Override
  public Boolean updateReplyComment(Comment comment) {
    sqlSession=MybatisUtil.getSqlSession();

    Boolean flag=sqlSession.getMapper(CommentMapper.class).updateReplyComment(comment);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }


}
